package com.yc.cloud.portal.controller;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.basic.dto.BaseQueryParam;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.entity.Order;
import com.yc.cloud.portal.request.OrderCreateParam;
import com.yc.cloud.portal.service.OrderService;
import com.yc.cloud.portal.vo.OrderCreateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 订单
 *
 * @author Yancy
 * @date 2024-01-18
 */
@RestController
@RequestMapping("/orders")
@Api(value = "订单", tags = "订单接口")
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping
    @ApiOperation(value = "订单列表", notes = "支持分页查询,字段模糊匹配,排序等")
    public ResponseResult<CommonPage<Order>> list(BaseQueryParam param) {
        val result = orderService.list(param);
        return ResponseResult.success(CommonPage.restPage(result));
    }


//    @GetMapping("{id}")
//    @ApiOperation(value = "订单详情", notes = "根据ID查询")
//    public ResponseResult<Order> get(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
//        val result = orderService.get(id);
//        return ResponseResult.success(result);
//    }


    @PostMapping
    @ApiOperation(value = "添加订单", notes = "返回业务订单信息和第三方支付订单信息")
    public ResponseResult<OrderCreateVo> create(@Validated @RequestBody OrderCreateParam param) {
        val result = orderService.create(param);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed();
    }


    @PostMapping("pay/notify")
    @ApiIgnore
    public synchronized String payNotify(HttpServletRequest request) {
        return orderService.payNotify(request);
    }


    //    @ApiOperation(value = "删除订单", notes = "通过ID删除")
//    @DeleteMapping(value = "{id}")
//    public ResponseResult<Boolean> delete(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
//        val result = orderService.delete(id);
//        return result ? ResponseResult.success(true) : ResponseResult.failed();
//    }
//
//
    @ApiOperation(value = "取消订单", notes = "通过ID修改")
    @PutMapping(value = "{id}/cancel")
    public ResponseResult<Boolean> cancelOrder(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = orderService.cancelOrder(id);
        return result ? ResponseResult.success(result) : ResponseResult.failed();
    }
}
